//
//  MSHomeHeadView.m
//  testMasory
//
//  Created by xiangbo on 2018/9/19.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSHomeHeadView.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MSHomeHeadView ()<CLLocationManagerDelegate>

@property(nonatomic,retain)CLLocationManager *locationManager;
@property (nonatomic, strong)UIButton *locationButton;
//@property(nonatomic,strong)UILabel *locationDescLabel;
//@property(nonatomic,strong)UILabel *locationLabel;
//@property(nonatomic,strong)UIButton *weatherButton;
@property(nonatomic,strong)UIButton *searchButton;

@property (nonatomic, assign) MSHeadViewType headType; // 页面类型

@end

@implementation MSHomeHeadView


- (instancetype)initWithFrame:(CGRect)frame headType:(MSHeadViewType)headType {
    self = [super initWithFrame:frame];
    if (self) {
        _headType = headType;
        [self setupViews];
    }
    return self;
}

- (void)setupViews{
    // 定位提示标签
    self.locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.locationButton setImage:[UIImage imageNamed:@"nav_adress"] forState:UIControlStateNormal];
    NSString *currentLoc = [[MSLocationManager shareInstance] currentLocalCityString] ?:@"定位失败";
    [self.locationButton setTitle:currentLoc forState:UIControlStateNormal];
    self.locationButton.titleLabel.font = [UIFont systemFontOfSize:16.f];
    [self.locationButton.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.locationButton);
        make.centerY.equalTo(self.locationButton);
    }];
    [self.locationButton.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.locationButton.imageView.mas_right);
        make.centerY.equalTo(self.locationButton);
    }];
    [self.locationButton setTitleColor:[UIColor colorWithHexString:@"222222"] forState:UIControlStateNormal];

    [self.locationButton setTitleColor:[UIColor colorWithHexString:@"222222"] forState:UIControlStateNormal];
    
    [self addSubview:self.locationButton];
    
    [self.locationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self).offset(21);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(118);
    }];

    CGFloat searchButtonHeight = 30;
    if (self.headType == MSHeadViewTypeFood) {
        // 美食页面需要搜索框，而酒店页面不需要
        // 搜索按钮
        // 搜索按钮的高度
        self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.searchButton.layer.cornerRadius = searchButtonHeight / 2;
        self.searchButton.layer.borderWidth = 0.0;
        self.searchButton.layer.borderColor = [UIColor clearColor].CGColor;
        [self.searchButton setBackgroundColor:[UIColor colorWithHexString:@"#F6F6F6"]];
        
        // 搜索图片
        UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_icon"]];
        [self.searchButton addSubview:searchIcon];
        // 搜索的提示标签
        UILabel *placeholderLabel = [[UILabel alloc] init];
        placeholderLabel.font = [UIFont systemFontOfSize:12];
        placeholderLabel.text = @"搜索";
        placeholderLabel.textColor = [UIColor colorWithHexString:@"#888888"];
        [self.searchButton addSubview:placeholderLabel];
        // 搜索按钮的点击事件
        [self.searchButton addTarget:self action:@selector(seachAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.searchButton];
        
        [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.locationButton.mas_bottom).offset(16);
            make.left.equalTo(self.locationButton);
            make.right.equalTo(self).offset(-20);
            make.height.mas_equalTo(searchButtonHeight);
        }];
        
        [placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.searchButton.mas_centerX).offset(-CGRectGetWidth(searchIcon.frame) / 2);
            make.centerY.equalTo(self.searchButton);
        }];
        
        [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.searchButton);
            make.right.equalTo(placeholderLabel.mas_left);
            make.width.mas_equalTo(CGRectGetWidth(searchIcon.frame));
            make.height.mas_equalTo(CGRectGetHeight(searchIcon.frame));
        }];
    }
}

/** 搜索按钮点击 */
- (void)seachAction:(id)sender {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(showSearchViewController)]) {
        [self.customDelegate showSearchViewController];
    }
}

#pragma mark - Public Method

- (void)refreshLocalTitle {
    NSString *currentLoc = [[MSLocationManager shareInstance] currentLocalCityString] ?:@"定位失败";
    [self.locationButton setTitle:currentLoc forState:UIControlStateNormal];
}

#pragma mark - Private Method
/** 展示错误提示 */
- (void)showErrorTipWithErrorMsg:(NSString *)errorMsg {
    [MBProgressHUD showDefaultHUDWithText:errorMsg toView:self.superview];
}


@end
